//
//  NSAttributedString+Emotion.m
//  Conversation
//
//  Created by C_HAO on 15/9/25.
//  Copyright © 2015年 CHAOO. All rights reserved.
//

#import "NSAttributedString+Emotion.h"
#import "JSQEmotionTextAttachment.h"

@implementation NSAttributedString (Emotion)

- (NSString *)plainEmotionString {
    NSMutableString *plainEmotionString = [NSMutableString stringWithString:self.string];
    __block NSUInteger base = 0;

    [self enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, self.length)
                     options:0
                  usingBlock:^(id value, NSRange range, BOOL *stop) {
        if (value && [value isKindOfClass:[JSQEmotionTextAttachment class]]) {
            [plainEmotionString replaceCharactersInRange:NSMakeRange(range.location + base, range.length)
                                              withString:((JSQEmotionTextAttachment *)value).emotionMark];
            base += ((JSQEmotionTextAttachment *)value).emotionMark.length - 1;
        }
    }];

    return plainEmotionString;
}

@end
